﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.app;
using System.Collections;
using System.IO;
using TravianHelper.hero;



namespace TravianHelper.tools
{
    public partial class SearchAddNatars2FarmList : Form
    {
         

        private ArrayList tempNatars = new ArrayList();

        public SearchAddNatars2FarmList()
        {
            InitializeComponent();
        }

        private void buttonSearchAdd_Click(object sender, EventArgs e)
        {
            try
            {
                TravianApplicationContext.allNatars = Util.DeSerializeArrayList("Natars.xml");
            }
            catch (Exception ex)
            {
                TravianApplicationContext.allNatars = new ArrayList();
            }


            int x = Convert.ToInt32(textBoxX.Text);
            int y = Convert.ToInt32(textBoxY.Text);
            for (int i = 0; i < TravianApplicationContext.allNatars.Count; i++)
            {
                Zone zone = (Zone)TravianApplicationContext.allNatars[i];
                zone.distance = Math.Sqrt((zone.x - x) * (zone.x - x) + (zone.y - y) * (zone.y - y));
                double maxDist = Convert.ToInt32(textBoxMaxDist.Text);
                double minDist = Convert.ToDouble(textBoxMinDist.Text);
                if (minDist <= zone.distance && zone.distance <= maxDist)
                {
                    tempNatars.Add(zone);
                }
            }
            updateListViewTarget(tempNatars);
        }

        private void updateListViewTarget(ArrayList zones)
        {
            listViewTarget.Items.Clear();
            foreach (Zone zone in zones)
            {
                listViewTarget.Items.Add(new ListViewItem(new string[] { zone.x.ToString() + "," + zone.y.ToString(), zone.distance.ToString(), zone.status, zone.population.ToString() }));
            }
            totalOasis.Text = "Total natars: " + zones.Count;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            tempNatars.Clear();
            updateListViewTarget(tempNatars);
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            string[] tmp = comboBoxTroopType.SelectedItem.ToString().Split('_');
            int id = Convert.ToInt32(tmp[0]);
            
            for (int i = 0; i < tempNatars.Count; i++)
            {
                Zone zone = (Zone)tempNatars[i];
                try
                {
                    int n = (int) (Convert.ToDouble(textBoxTroopNumber.Text) * zone.population / 100);

                    TravianApplicationContext.adapter.addToFarmList(textBoxLinkAddFarm.Text, zone.x, zone.y, id, n);
                }
                catch (Exception ex)
                {

                    Console.WriteLine("error add farm " + ex.Message + ex.StackTrace);
                }
                
               
            }
            MessageBox.Show("Natars added to farm list");
        }

        private void label16_Click(object sender, EventArgs e)
        {

        }
    }
}
